import 'package:dio/dio.dart';
import 'package:new_yuqib_app/yqbConfig/yqb_app.dart';
import 'package:new_yuqib_app/yqbConfig/yqb_base.dart';
import 'package:new_yuqib_app/yqbModel/apply_data_model.dart';
import 'package:new_yuqib_app/yqbModel/im_login_data_model.dart';
import 'package:new_yuqib_app/yqbModel/login_model.dart';
import 'package:new_yuqib_app/yqbModel/video_data_model.dart';
import 'package:new_yuqib_app/yqbModel/xie_yi_model.dart';

import 'yqb_dio_request.dart';
import 'yqb_req_api.dart';

///请求方法
class ApiMethods {
  //初始化
  static Future init() async {
    YqbDioRequest.postRequest(ReqApi.init).then((value) {});
  }

  //激活
  static Future activation() async {
    final oaid = await YqbApp.oaid;
    var putData = {
      'pkg': YqbApp.packageInfo.packageName,
      'imei': "",
      'oaid': oaid,
      "channel": channel,
    };
    return await YqbDioRequest.postRequest(ReqApi.activation,
        queryParameters: putData);
  }

  //发送验证码
  static Future getCaptcha(String phone) async {
    var putData = {
      'phone': phone,
      'type': "1",
      'pkg': YqbApp.packageInfo.packageName,
      "channel": channel,
    };
    return await YqbDioRequest.postRequest(ReqApi.getCaptcha,
        queryParameters: putData);
  }

  //登录
  static Future<LoginModel> login(Map<String, dynamic> putData) async {
    // 'phone': _textEditingControllerPhonenumber.text,
    // 'captcha': _textEditingControllerPhoneCode.text,
    final oaid = await YqbApp.oaid;
    var data = {
      'nickName': "yuqibang",
      'pkg': YqbApp.packageInfo.packageName,
      'imei': "",
      'oaid': oaid,
      "channel": channel,
      ...putData,
    };
    var reqData =
        await YqbDioRequest.postRequest(ReqApi.login, queryParameters: data);
    return LoginModel.fromJson(reqData.data ?? {});
  }

  //注销
  static Future zhuxiao() async {
    return await YqbDioRequest.postRequest(ReqApi.zhuxiao);
  }

  //协议
  static Future<XieYiModel> xieyi() async {
    var reqData = await YqbDioRequest.postRequest(ReqApi.getAgreement);
    return XieYiModel.fromJson(reqData.data ?? {});
  }

  ///图片上传
  static Future<String> uploadImage(String filePath) async {
    final data =
        FormData.fromMap({"file": MultipartFile.fromFileSync(filePath)});
    final res = await YqbDioRequest.dio.post(ReqApi.upLoad, data: data);
    return res.data["data"]["fullurl"] ?? "";
  }

  ///反馈
  static Future feedback(Map<String, dynamic> data) async {
    final res = await YqbDioRequest.postRequest(ReqApi.complaintApp,
        queryParameters: data);
    return XieYiModel.fromJson(res.data ?? {});
  }

  ///提交报名
  static Future<ApplyDataModel> createApply(
      Map<String, dynamic> putData) async {
    final data = {
      'service_id': "",
      'age_range': "28",
      "username": "",
      "other_touch": "",
      "mobile_time": "",
      "province": "重庆",
      "city": "重庆",
      ...putData
    };
    final res = await YqbDioRequest.postRequest(ReqApi.createApply,
        queryParameters: data);
    return ApplyDataModel.fromJson(res.data);
  }

  ///发送报名消息
  static Future enterChat(String id) async {
    final data = {"service_id": id};
    final res = await YqbDioRequest.postRequest(ReqApi.enterChat,
        queryParameters: data);
    return res;
  }

  ///Im登录
  static Future<ImLoginDataModel> imChatGetSig() async {
    final res = await YqbDioRequest.postRequest(ReqApi.imChatGetSig);
    return ImLoginDataModel.fromJson(res.data);
  }

  ///注销
  static Future logOut() async {
    final res = await YqbDioRequest.postRequest(ReqApi.zhuxiao);
    return res;
  }

  //视频列表
  static Future<List<VideoDataModel>> videoList([int page = 1]) async {
    final data = {
      "page": page,
      "pagesize": "10",
    };
    final res = await YqbDioRequest.postRequest(ReqApi.videoList,
        queryParameters: data);
    return VideoDataModel.fromList(res.data??[]);
  }

  //案例
  static Future caseExample() async {
    final res = await YqbDioRequest.postRequest(ReqApi.caseExample);
    return res;
  }
}
